package com.intellagric.service.iftmonitor.Impl.ift_warning_config;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.IftWarningConfigMapper;
import com.intellagric.pojo.IftWarningConfig;
import com.intellagric.pojo.IftWarningConfigExample;
import com.intellagric.service.iftmonitor.ift_warning_config.WarningConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/25
 * @DESC： 预警阈值配置信息服务实现
 */
@Service
public class WarningConfigServiceImpl implements WarningConfigService {

    @Autowired
    private IftWarningConfigMapper warningConfigMapper;

    /**
     * 添加预警阈值配置信息
     * @param warningConfig
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addWarningConfig(IftWarningConfig warningConfig) {
        warningConfig.setId(UUID.randomUUID().toString().replaceAll("-",""));
        warningConfig.setCreateDate(new Date());
        warningConfig.setUpdateDate(new Date());
        if (warningConfigMapper.insertSelective(warningConfig) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除预警阈值配置信息
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteWarningConfig(String id) {
        if (warningConfigMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 编辑预警阈值配置信息
     * @param warningConfig
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editWarningConfig(IftWarningConfig warningConfig) {
        warningConfig.setUpdateDate(new Date());
        if (warningConfigMapper.updateByPrimaryKeySelective(warningConfig) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询预警阈值配置信息
     * @param id
     * @return IftWarningConfig
     */
    @Override
    public IftWarningConfig getWarningConfigById(String id) {
        return warningConfigMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询预警阈值配置信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getWarningConfigList() {
        IftWarningConfigExample warningConfigExample = new IftWarningConfigExample();
        warningConfigExample.createCriteria().andIdIsNotNull();
        List<IftWarningConfig> warningConfigList = warningConfigMapper.selectByExample(warningConfigExample);
        return LayuiDataGridResult.success().add(warningConfigList,warningConfigList.size());
    }

    /**
     * 通过场景id查询预警阈值配置信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getWarningConfigListByScenery(String sceneId) {
        IftWarningConfigExample warningConfigExample = new IftWarningConfigExample();
        warningConfigExample.createCriteria().andSceneIdEqualTo(sceneId);
        List<IftWarningConfig> warningConfigList = warningConfigMapper.selectByExample(warningConfigExample);
        return LayuiDataGridResult.success().add(warningConfigList,warningConfigList.size());
    }

    /**
     * 通过场景id和采集项id查询预警阈值配置信息列表
     * @param sceneId
     * @param channelId
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getWarningConfigListBySceneryAndChannel(String sceneId, String channelId) {
        IftWarningConfigExample warningConfigExample = new IftWarningConfigExample();
        warningConfigExample.createCriteria().andSceneIdEqualTo(sceneId).andCheckItemIdEqualTo(channelId);
        List<IftWarningConfig> warningConfigList = warningConfigMapper.selectByExample(warningConfigExample);
        return LayuiDataGridResult.success().add(warningConfigList,warningConfigList.size());
    }

    /**
     * 分页查询预警阈值配置信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getWarningConfigPage(int page, int rows) {
        IftWarningConfigExample warningConfigExample = new IftWarningConfigExample();
        warningConfigExample.createCriteria().andIdIsNotNull();
        List<IftWarningConfig> warningConfigList = warningConfigMapper.selectByExample(warningConfigExample);
        return LayuiDataGridResult.success().add(warningConfigList,this.getWarningConfigList().getCount());
    }

}
